[1] 4
Jürgen Wilbert
University of Potsdam
10/12/22
mean, sqrt).function_name(argument_name_1 = value, argument_2 = value, ...)
sqrt() calculates the square root
You can jump through your code by the list a the bottom of your source panel or, after activating the outline panel, at the right of your source panel:
… or use the bottom-right help panel in R Studio
Take a look at the mean() function (hint: use the help() function):
What arguments could be specified?
:-)
Operations are a special kind of functions that have a shortcut.
.
.
Objects have an object name and contain data.
The data are assigned to an object with the <- or = operator.
You can see the value(s) of an object with the print() function, or by just typing the object name:
Objects can be used for operators and arguments in functions:
You can write the return values of a function into a new object:
And you can combine these:
Assign the values 40 and 24 to the variables a and b.
Calculate the square root of the sum of a and b.
:-)
Assign the values 40 and 24 to the variables a and b.
Calculate the square root of the sum of a and b.
The data of objects can be numbers, text or TRUE/FALSE values. These are called data types
1, 1.35"A", 'House'TRUE, FALSEData are organized in structures:
Data are organized in structures:
You create a vector with the c() function:
The colon : operator creates a numerical sequence:
This is a shortcut for seq(1, 10)
You can build a vector of any data type:
But do not mix data types in a vector. You will get an error or they are internally changed to fit one data type:
Here 10 and 12 are changed to a character data type "10" and "12".
Create a vector (named friends comprising four names of your friends.
:-)
Create a vector (named friends comprising four names of your friends.
When an object is a vector it can be reused within the c() function to build a new vector:
Be careful not to confuse an object name with a character:
Create two vectors
x <- c("I", "am", "a") and
y <- "person!"
Now create a new vector by reusing x and y that looks like this when printed:
[1] "I" "Am" "a" "cool" "person!"
:-)
Use the paste() function to create a single string.
The argument collapse defines a character string to set between elements:
A missing value is represented with NA (Not Available).
2, 5, 7, 4, 7, 2, 6.2, NA, 7, 4, NA, 2, 6.Calculate the mean of these two vectors.
Note: Use the mean() function to calculate the mean.
Note: Read through ?mean if you encounter problems.
:-)
A named vector is a vector with a name for each element:
James Hella Armin
34 30 43
James Hella Armin
TRUE FALSE TRUE
You get and set the names of a named object with the names() argument:
the as functions convert vectors between data types:
[1] "1" "2" "3" "4" "5"
[1] 0 1 0
[1] FALSE TRUE FALSE TRUE TRUE
[1] 4711 814 7
A factor is a vector with labels for vector levels.
A factor is created with the factor() function.
The levels argument defines the possible factor levels.
The labels argument defines the corresponding labels.
Example:
Build a factor for gender with the labels male, female, non-binary. Include a vector for six fictitious gender values.
:-)
Build a factor for gender with the labels male, female, non-binary. Include a vector for six fictitious gender values.
Data frames are the standard object for storing research data. They contain variables (columns) and cases (rows). A data frame is created with the data.frame() function.
# For better convenience I have inserted additional linebreaks and spaces
study <- data.frame(
sen = c(0, 1, 0, 1, 0, 1),
gender = c("M", "M", "F", "M", "F", "F"),
age = c(12, 13, 11, 10, 11, 14),
IQ = c(90, 85, 90, 87, 99, 89)
)
study| sen | gender | age | IQ |
|---|---|---|---|
| 0 | M | 12 | 90 |
| 1 | M | 13 | 85 |
| 0 | F | 11 | 90 |
| 1 | M | 10 | 87 |
| 0 | F | 11 | 99 |
| 1 | F | 14 | 89 |
Variables within a data frame are extracted with double square brackets.
An alternative approach is to use the $ sign:
Lists are the most versatile data structures in R and are very important for understanding R.
A list is a series of elements with arbitrary data types and structures. A list is constructed with the list() function
It is best to name list elements:
You can extract a list element with [[ or $ signs:
lists can be very complex with lists nested in lists:
The str() function returns the structure of an R object
List of 2
$ list :List of 2
..$ A: num 1
..$ B: int [1:3] 1 2 3
$ list_in_list:List of 2
..$ C:List of 1
.. ..$ D: num 4
..$ E: num 5
Jürgen Wilbert - Introduction to R
Comments
It is a good practice to add comments and notes to your code.
Everything that is written behind a
#will not executed as code.If you want a comment to span across several line, you have to begin each line with a
#symbol.Within RStudio, you can use comments to create headers to navigate through your code by ending a comment line with four
-signs: